﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class reEmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["userID"] == null)
        {
            Response.Redirect("notLogin.html", true);
            return;
        }
        else if (Request.Cookies["passwordProtectionStatus"].Value == "1")
        {
            main.InnerHtml = "您的密码保护信息还不完善，请先去完善信息<br>我们会在您完善信息之后发送验证邮件<br>" +
                             "<a href=\"passwordProtection.aspx\">去完善密保信息</a>&nbsp;&nbsp;&nbsp;<a href=\"welcome.aspx\">返回欢迎页面</a>";
            return;
        }
        else if (Request.Cookies["passwordProtectionStatus"] == null)
        {
            main.InnerHtml = "您已经启用了密码保护功能，无需再次访问本页面<br><a href=\"welcome.aspx\">返回欢迎页面</a>";
            return;
        }
    }
    protected void reEmailButton_Click(object sender, EventArgs e)
    {
        //从数据库中获取邮箱地址
        ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["connectionString"];
        using (SqlConnection dataBaseConnection = new SqlConnection(settings.ConnectionString))
        {
            dataBaseConnection.Open();

            string userID = Request.Cookies["userID"].Value;

            string quesyString = "SELECT email " +
                                 "FROM users " +
                                 "WHERE userID = @userID";
            SqlCommand command = new SqlCommand(quesyString, dataBaseConnection);
            SqlParameter userIDParameter = new SqlParameter("@userID", userID);
            command.Parameters.Add(userIDParameter);
            SqlDataReader reader;
            try
            {
                reader = command.ExecuteReader();
            }
            catch (Exception exception)
            {
                main.InnerHtml = "数据库方面发生了错误，请联系我们以解决 AFTER GET emailAddress " + exception.Message;
                return;
            }

            reader.Read();
            string emailAddress = reader["email"].ToString();

            //发送邮件
            try
            {
                Tools.SendVerifyEmail(emailAddress, userID, Request.Cookies["username"].Value);
            }
            catch (Exception exception)
            {
                main.InnerHtml = "在发送邮件时出现了错误，请联系我们以解决 " + exception.Message;
                return;
            }

            //发送成功时重定向到emailSent.aspx
            Response.Redirect("emailSent.aspx?emailAddress=" + emailAddress);
        }
    }
}